{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "损失为0.6931471805599453\n",
      "损失为0.5912894260003538\n",
      "损失为0.5557961107127088\n",
      "损失为0.5289765131562365\n",
      "损失为0.5068812917435517\n",
      "损失为0.48787986321716575\n",
      "损失为0.47110827803124367\n",
      "损失为0.4560458096982852\n",
      "损失为0.4423502279336529\n",
      "损失为0.4297817153507784\n",
      "损失为0.41816382093643273\n",
      "损失为0.40736174995821917\n",
      "损失为0.3972694687269799\n",
      "损失为0.3878016072295409\n",
      "损失为0.3788881303593957\n",
      "损失为0.3704706898360003\n",
      "损失为0.36250004228346355\n",
      "损失为0.35493416988240534\n",
      "损失为0.34773687946679727\n",
      "损失为0.3408767368074387\n",
      "测试集准确率为68.0%\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xl8VPW9//HXJwmEHQIJWxIIW4ogm4RFwF0UrIJ1K9gFayv1Vtva2ttrr/dXe+29t2o3W7WutdW64FJtcSviBoIihH2HELawBhJ2CCT5/P7IgY5pAhOYZJKZ9/PxmEfmfOd75nzmzOQ9Z85q7o6IiMSHhGgXICIidUehLyISRxT6IiJxRKEvIhJHFPoiInFEoS8iEkcU+iIicUShLyISRxT6IiJxJCnaBVSWmprqWVlZ0S5DRKRBmT9//i53TztVv3oX+llZWeTm5ka7DBGRBsXMNobTT6t3RETiSFihb2ZjzGy1meWZ2V1VPP5bM1sU3NaY2Z6QxyaZ2drgNimSxYuISM2ccvWOmSUCjwCjgQJgnplNdfcVx/u4+w9C+n8XGBTcbwvcA+Q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6Htt8P4uBhYAV4U8Tw4VAbwOeJjggMxaqK3G712k/2erqito/zNwa6W+dTLPqD4bovYZ0xG5IiJxJFZW74iISBgU+iIicUShLyISRxT6IiJxRKEvIhJHFPoiInFEoS8iEkcU+iIiceT/A22cmSC86WnHAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import h5py \n",
    "from skimage import transform\n",
    "\n",
    "# 加载相应的数据集\n",
    "def load_dataset():\n",
    "    train_dataset = h5py.File('datasets/train_catvnoncat.h5', \"r\")\n",
    "    train_dataset_x = train_dataset['train_set_x'][:]\n",
    "    train_dataset_y = train_dataset['train_set_y'][:]\n",
    "    test_dataset = h5py.File('datasets/test_catvnoncat.h5', \"r\")\n",
    "    test_dataset_x = test_dataset['test_set_x'][:]\n",
    "    test_dataset_y = test_dataset['test_set_y'][:]\n",
    "    classes = test_dataset['list_classes'][:]\n",
    "    \n",
    "#     print(np.array(train_dataset_y), type(train_dataset_x))\n",
    "    train_dataset_y, test_dataset_y = train_dataset_y.reshape((1, train_dataset_y.shape[0])), test_dataset_y.reshape((1, test_dataset_y.shape[0])) \n",
    "    train_dataset_x = train_dataset_x.reshape((train_dataset_x.shape[0], -1)).T/255\n",
    "    test_dataset_x = test_dataset_x.reshape((test_dataset_x.shape[0], -1)).T/255\n",
    "    \n",
    "    return train_dataset_x, train_dataset_y, test_dataset_x, test_dataset_y, classes\n",
    "#sigmoid函数\n",
    "def sigmoid(z):\n",
    "    return 1 / (1 + np.exp(-z))\n",
    "#反向传播算法\n",
    "def propogate(w, b, X, Y):\n",
    "    Z = np.dot(w.T, X) + b\n",
    "    A = sigmoid(Z)\n",
    "    m = X.shape[1]\n",
    "    J = np.sum(-(Y*np.log(A)+(1-Y)*np.log(1-A)))/m\n",
    "    dZ = A - Y\n",
    "    dw = np.dot(X, dZ.T)/m\n",
    "    db = np.sum(dZ)/m\n",
    "    return dw ,db, J\n",
    "#优化过程\n",
    "def optimize(X, Y, iterations, learning_rate):\n",
    "    w = np.zeros((X.shape[0], 1))\n",
    "    b = 0\n",
    "    cost_list = []\n",
    "    for i in range(iterations):\n",
    "        dw, db, cost = propogate(w, b, X, Y)\n",
    "        w = w - learning_rate*dw\n",
    "        b = b - learning_rate*db\n",
    "        cost_list.append(cost)\n",
    "        if i%100 == 0:\n",
    "            print('损失为{0}'.format(cost))\n",
    "    return w, b, cost_list\n",
    "#进行相应的预测\n",
    "def predict(w, b, X, Y):\n",
    "    m = X.shape[1]\n",
    "    Y_predict = np.zeros((1,m))\n",
    "    A = sigmoid(np.dot(w.T, X)+b)\n",
    "    for j in range(A.shape[1]):\n",
    "        if A[0,j]>0.5:\n",
    "            Y_predict[0,j]=1\n",
    "    accuracy = np.sum(Y_predict == Y)/m\n",
    "    return Y_predict, accuracy\n",
    "#构建相应的模型\n",
    "def model(X_train,Y_train,X_test,Y_test,iterations,learning_rate):\n",
    "    w, b, cost_list = optimize(X_train,Y_train, iterations, learning_rate)\n",
    "    plt.plot(cost_list)\n",
    "    y_predict, acc = predict(w,b,X_test,Y_test) \n",
    "    print('测试集准确率为{0}%'.format(100*acc))\n",
    "    plt.show()\n",
    "    \n",
    "    \n",
    "train_dataset_x, train_dataset_y, test_dataset_x, test_dataset_y, classes = load_dataset()\n",
    "model_ = model(train_dataset_x, train_dataset_y, test_dataset_x, test_dataset_y, iterations=2000, learning_rate=0.001)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
